package com.cvc.tracker.es;

import org.opencv.android.Utils;
import org.opencv.core.Mat;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class CVCTrackerActivity extends Activity {
	
	static {
		System.loadLibrary("opencv_java");
	}

	private GL2View mViews;
	public static Capturer cap;
	public static Improc improc;
	public static Renderer rend;
	private Thread capThread;
	private Thread improcThread;
	
	private Mat mMarkerMat;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		cap = new Capturer();
		improc = new Improc();
		rend = new Renderer();
		capThread = new Thread(cap);
		improcThread = new Thread(improc);
		
		Bitmap mMarkerBmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.obidosrr );
		mMarkerMat = new Mat(); 
		Utils.bitmapToMat(mMarkerBmp, mMarkerMat);
		CVCTLib.loadMarker(mMarkerMat.nativeObj);

		mViews = new GL2View(this);
		setContentView(mViews);

	}

	@Override
	protected void onResume() {
		super.onResume();
		capThread.start();
		improcThread.start();
	}

	@Override
	protected void onPause() {
		super.onPause();
		cap.tellMeToStop();
		improc.tellMeToStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mMarkerMat.release();
		mMarkerMat = null;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// getMenuInflater().inflate(R.menu.activity_cvctracker, menu);
		menu.add(Menu.NONE, 1, 100, "Natural");
		menu.add(Menu.NONE, 2, 100, "Chessboard");
		menu.add(Menu.NONE, 3, 100, "Chessboard with KLT");
		menu.add(Menu.NONE, 4, 100, "Natural with KLT");
		menu.add(Menu.NONE, 5, 100, "Restart Klt");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {

		switch(item.getItemId())
		{
		case 1:
			improc.isKlt = 0;
			improc.isNatural = 1;
			break;
		case 2:
			improc.isKlt = 0;
			improc.isNatural = 0;			
			break;
		case 3:
			improc.isKlt = 1;
			improc.isNatural = 0;			
			break;
		case 4:
			improc.isKlt = 1;
			improc.isNatural = 1;			
			break;
		case 5:
			
			break;
		}
		improc.resetTrackingState();
		return true;
	}

}
